package com.imonsoft.sqm.dao.business.supplier_quality;

import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManage;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManageExample;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.vo.business.supplier_quality.EnvManageVo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
@SuppressWarnings("deprecation")
@Repository("envManageDAO")
public class EnvManageDAOImpl extends SqlMapClientDaoSupport implements EnvManageDAO {

	@Autowired
	private DictionaryService dictionaryService;
    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public EnvManageDAOImpl() {
        super();
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int countEnvManageByExample(EnvManageExample example) {
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("tt_env_manage.ibatorgenerated_countByExample", example);
        return count;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int deleteEnvManageByExample(EnvManageExample example) {
        int rows = getSqlMapClientTemplate().delete("tt_env_manage.ibatorgenerated_deleteByExample", example);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int deleteEnvManageByPrimaryKey(Long id) {
        EnvManage key = new EnvManage();
        key.setId(id);
        int rows = getSqlMapClientTemplate().delete("tt_env_manage.ibatorgenerated_deleteByPrimaryKey", key);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public Long insertEnvManage(EnvManage record) {
        Object newKey = getSqlMapClientTemplate().insert("tt_env_manage.ibatorgenerated_insert", record);
        return (Long) newKey;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public Long insertEnvManageSelective(EnvManage record) {
        Object newKey = getSqlMapClientTemplate().insert("tt_env_manage.ibatorgenerated_insertSelective", record);
        return (Long) newKey;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    @SuppressWarnings("unchecked")
    public List<EnvManage> selectEnvManageByExample(EnvManageExample example) {
        List<EnvManage> list = getSqlMapClientTemplate().queryForList("tt_env_manage.ibatorgenerated_selectByExample", example);
        return list;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public EnvManage selectEnvManageByPrimaryKey(Long id) {
        EnvManage key = new EnvManage();
        key.setId(id);
        EnvManage record = (EnvManage) getSqlMapClientTemplate().queryForObject("tt_env_manage.ibatorgenerated_selectByPrimaryKey", key);
        return record;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int updateEnvManageByExampleSelective(EnvManage record, EnvManageExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("tt_env_manage.ibatorgenerated_updateByExampleSelective", parms);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int updateEnvManageByExample(EnvManage record, EnvManageExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("tt_env_manage.ibatorgenerated_updateByExample", parms);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int updateEnvManageByPrimaryKeySelective(EnvManage record) {
        int rows = getSqlMapClientTemplate().update("tt_env_manage.ibatorgenerated_updateByPrimaryKeySelective", record);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    public int updateEnvManageByPrimaryKey(EnvManage record) {
        int rows = getSqlMapClientTemplate().update("tt_env_manage.ibatorgenerated_updateByPrimaryKey", record);
        return rows;
    }

    /**
     * This class was generated by Apache iBATIS ibator.
     * This class corresponds to the database table tt_env_manage
     *
     * @ibatorgenerated Mon Jun 03 13:55:26 CST 2013
     */
    private static class UpdateByExampleParms extends EnvManageExample {
        private Object record;

        public UpdateByExampleParms(Object record, EnvManageExample example) {
            super(example);
            this.record = record;
        }

        public Object getRecord() {
            return record;
        }
    }

	@SuppressWarnings("unchecked")
	@Override
	public DataGrid<EnvManageVo> getDataGridVoBy(EnvManage envManage, Pager pager) {
	
		EnvManageExample example = new EnvManageExample();
		EnvManageExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
		if (!StringUtil.isBlank(StringUtil.fixup(envManage.getSupplierId()))){
			criteria.andSupplierIdEqualTo(envManage.getSupplierId());	
		}
		Integer count=countEnvManageByExample(example);
		List<EnvManage> rows =getSqlMapClientTemplate().queryForList("tt_env_manage.ibatorgenerated_selectByExample", example, pager.getSkipResults(), pager.getRows());
		List<EnvManageVo> list=new ArrayList<EnvManageVo>();
		for (EnvManage envManage2 : rows) {
			EnvManageVo vo=new EnvManageVo();
			BeanUtils.copyProperties(envManage2, vo);
			vo.setCertificationDates(DateUtil.date2string(envManage2.getCertificationDate(), DateUtil.DATE_FORMAT));
			vo.setExpiredDates(DateUtil.date2string(envManage2.getExpiredDate(), DateUtil.DATE_FORMAT));
			vo.setMailDates(envManage2.getMailDate());
			
			List<Dictionary> d1=dictionaryService.findDictionaryByType(ConstantDict.ENVMANAGE_RZLX);
			for (Dictionary dictionary : d1) {
				if(dictionary.getCode().equals(vo.getCertificationType())){
					vo.setCertificationType(dictionary.getName());
				}
			}
			
			List<Dictionary> d2=dictionaryService.findDictionaryByType(ConstantDict.ENVMANAGE_XD);
			for (Dictionary dictionary : d2) {
				if(dictionary.getCode().equals(vo.getUpdateRequired())){
					vo.setUpdateRequired(dictionary.getName());
				}
			}
			
			list.add(vo);
		}
		
		DataGrid<EnvManageVo> dataGrid = new DataGrid<EnvManageVo>(count, list);
		return dataGrid;
	}
}